﻿using System;
using Colligate;

namespace OrgDataSource
{
    public class OrgModel : BaseTaskDataSource
    {
        private readonly TaskInfoConfig myConfig;
        private const string connStr = "Data Source=10.8.64.195;Initial Catalog=SZJT;User ID=sa;Password=1";
        public OrgModel()
        {
            myConfig = new TaskInfoConfig();
        }
        public override TaskInfoConfig ConfigInfo
        {
            get
            {
                return myConfig;
            }
        }


        public override int InitTaskData()
        {
            return 0;
        }

        public override int DeleteTaskData(OUDataInfo UserInfo, string DataGuid, string DataType)
        {
            try
            {
#if DEBUG
                using (var context = new orgEntities(Utility.GetConnStr(connStr)))
#else
                using (var context = new orgEntities(Utility.GetConnStr(this.ConfigInfo.ConnectString)))
#endif
                //using (var context = new orgEntities(Utility.GetConnStr(ConfigInfo.ConnectString)))
                {
                    var ds = DataSourceFactory.GetService(DataType);
                    return ds.DeleteTaskData(UserInfo, DataGuid, DataType, context);
                }
            }
            catch (Exception e)
            {
                EnvironmentInfo.WriteLogInfo(e.Message, DebugLevel.CodePoint);
                return 1;
            }
        }

        public override void DoHasten()
        {
            
        }

        public override System.Xml.XmlNode GetTaskData(OUDataInfo UserInfo, string DataGuid, string DataType)
        {
            try
            {
#if DEBUG
                using (var context = new orgEntities(Utility.GetConnStr(connStr)))
#else
                using (var context = new orgEntities(Utility.GetConnStr(this.ConfigInfo.ConnectString)))
#endif
                //using (var context = new orgEntities(Utility.GetConnStr(ConfigInfo.ConnectString)))
                {
                    var ds = DataSourceFactory.GetService(DataType);
                    return ds.GetTaskData(UserInfo, DataGuid, DataType, context, this);
                }
            }
            catch (Exception e)
            {
                EnvironmentInfo.WriteLogInfo(e.Message, DebugLevel.CodePoint);
                return null;
            }
        }

        public override int SetTaskData(OUDataInfo UserInfo, string DataGuid, string DataType, System.Xml.XmlNode XmlData)
        {
            try
            {
#if DEBUG
                using (var context = new orgEntities(Utility.GetConnStr(connStr)))
#else
                using (var context = new orgEntities(Utility.GetConnStr(this.ConfigInfo.ConnectString)))
#endif
                //using (var context = new orgEntities(Utility.GetConnStr(ConfigInfo.ConnectString)))
                {
                    var ds = DataSourceFactory.GetService(DataType);
                    return ds.SetTaskData(UserInfo, DataGuid, DataType, XmlData, context);
                }
            }
            catch (Exception e)
            {
                EnvironmentInfo.WriteLogInfo(e.Message, DebugLevel.CodePoint);
                return 1;
            }
        }

        public override System.Xml.XmlNode TransformTaskData(OUDataInfo UserInfo, System.Xml.XmlNode xmlParam)
        {
            try
            {
#if DEBUG
                using (var context = new orgEntities(Utility.GetConnStr(connStr)))
#else
                using (var context = new orgEntities(Utility.GetConnStr(this.ConfigInfo.ConnectString)))
#endif
                //using (var context = new orgEntities(Utility.GetConnStr(ConfigInfo.ConnectString)))
                {
                    var dataType = XmlNodeManage.GetSubValue(xmlParam, "@DataType");
                    var ds = DataSourceFactory.GetService(dataType);
                    return ds.TransformTaskData(UserInfo, xmlParam, context, this);
                }
            }
            catch (Exception e)
            {
                EnvironmentInfo.WriteLogInfo(e.Message, DebugLevel.CodePoint);
                return null;
            }
        }
    }
}
